<template name="collection">
	<view>
		<!-- 顶部轮播图 -->
		<swiper class="screen-swiper round-dot" :indicator-dots="true" :circular="true" :autoplay="true" interval="5000"
		 duration="500" indicator-active-color="#8c7866">
			<swiper-item v-for="(item,index) in bannerList" :key="index">
				<image :src="item.img" mode="heightFix"></image>
			</swiper-item>
		</swiper>
		
		
		<view class="VerticalBox">
			<!-- 左侧垂直列表 -->
			<scroll-view class="VerticalNav nav" scroll-y scroll-with-animation :scroll-top="verticalNavTop" style="height:calc(100vh - 375upx)">
				<view class="cu-item" :class="index==tabCur?'text-darkgold cur':''" v-for="(item,index) in list" :key="index" @tap="TabSelect"
				 :data-id="index">
					{{item.name}}
				</view>
			</scroll-view>
			
			<!-- 右侧信息 -->
			<scroll-view class="VerticalMain" scroll-y scroll-with-animation style="height:calc(100vh - 375upx)"
			 :scroll-into-view="'main-'+mainCur" @scroll="VerticalMain">
				<view class="padding-top padding-lr" v-for="(item,index) in list" :key="index" :id="'main-'+index">
					<view class="cu-bar solid-bottom bg-white">
						<view class="action sub-title">
							<text class="text-bold text-darkgold">{{item.name}}</text>
							<text class="bg-darkgold"></text>
						</view>
					</view>
					<view class="cu-list menu-avatar">
						<view class="cu-item " @click="selectDetail(detailItem.objectIndex)" v-for="(detailItem,detindex) in detailList[item.id].ObjectArray" :key="detindex" :id="'main-'+detindex">
							<!-- <text>{{objectList[detailItem.objectIndex].name}} </text> -->
							<view class="cu-avatar radius lg" :style="{backgroundImage:'url('+ objectList[detailItem.objectIndex].imageArray[0].url +')'}"></view>
							<view class="content">
								<view class="text-darkgold"><text class="text-cut">{{objectList[detailItem.objectIndex].name}}</text></view>
								<view class="text-gray text-sm flex"> <text class="text-cut">{{objectList[detailItem.objectIndex].baseInfo}}</text></view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import json from '@/json';
	export default {
		name: 'collection',
		data() {
			return {
				list: json.classifyList,
				tabCur: 0,
				mainCur: 0,
				verticalNavTop: 0,
				load: true,
				detailList:json.ClassObjectMapper, 
				bannerList: json.objectSwiperList,
				objectList:json.objectList
			};
		},
		// onLoad(option) {
		// 	uni.showLoading({
		// 		title: '加载中...',
		// 		mask: true
		// 	});
		// 	if (option.classifyId) {
		// 		this.tabCur = option.classifyId;
		// 		this.mainCur = option.classifyId;
		// 		this.verticalNavTop = (option.classifyId - 1) * 50
		// 	}
		// 	// this.listCur = this.list[0];
		// },
		// onReady() {
		// 	uni.hideLoading()
		// },
		props: {
			redirectIndex: {
				type: Number,
				default: 0
			}
		},
		created() {
			this.tabCur = this.redirectIndex
			this.mainCur = this.redirectIndex;
			this.verticalNavTop = (this.redirectIndex - 1) * 50
		},
		watch: {
			redirectIndex(newVal, oldVal) {
				this.tabCur = newVal;
				this.mainCur = newVal;
				this.verticalNavTop = (newVal - 1) * 50
			}
		},
		methods: {
			TabSelect(e) {
				this.tabCur = e.currentTarget.dataset.id;
				this.mainCur = e.currentTarget.dataset.id;
				this.verticalNavTop = (e.currentTarget.dataset.id - 1) * 50
			},
			VerticalMain(e) {
				// #ifdef MP-ALIPAY
				   return false  //支付宝小程序暂时不支持双向联动 
				// #endif
				let that = this;
				let tabHeight = 0;
				if (this.load) {
					for (let i = 0; i < this.list.length; i++) {
						let view = uni.createSelectorQuery().select("#main-" + this.list[i].id);
						view.fields({
							size: true
						}, data => {
							this.list[i].top = tabHeight;
							tabHeight = tabHeight + data.height;
							this.list[i].bottom = tabHeight;
						}).exec();
					}
					this.load = false
				}
				let scrollTop = e.detail.scrollTop + 10;
				for (let i = 0; i < this.list.length; i++) {
					if (scrollTop > this.list[i].top && scrollTop < this.list[i].bottom) {
						this.verticalNavTop = (this.list[i].id - 1) * 50
						this.tabCur = this.list[i].id
						return false
					}
				}
			},
			selectDetail(index) {
			    uni.navigateTo({
			     url:'../collection/collectionDetail?index=' + index
			    })
			   },
		},
	}
</script>

<style>
	.fixed {
		position: fixed;
		z-index: 99;
	}

	.VerticalNav.nav {
		width: 200upx;
		white-space: initial;
	}

	.VerticalNav.nav .cu-item {
		width: 100%;
		text-align: center;
		background-color: #fff;
		margin: 0;
		border: none;
		height: 50px;
		position: relative;
	}

	.VerticalNav.nav .cu-item.cur {
		background-color: #DDD7D1;
	}

	.VerticalNav.nav .cu-item.cur::after {
		content: "";
		width: 8upx;
		height: 30upx;
		border-radius: 10upx 0 0 10upx;
		position: absolute;
		background-color: currentColor;
		top: 0;
		right: 0upx;
		bottom: 0;
		margin: auto;
	}

	.VerticalBox {
		display: flex;
	}

	.VerticalMain {
		background-color: #f1f1f1;
		flex: 1;
	}
	.cu-list.menu-avatar>.cu-item .content {
		position: absolute;
		left: 146rpx;
		width: calc(100% - 96rpx - 60rpx - 20rpx);
		line-height: 1.6em;
	}

</style>
